<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '0px 10px 5px' }">
        <div slot="header" class="f16">
          <div style="font-weight: 600; margin-top: 10px">五金其他入库查询</div>
        </div>
        <Table :tableHeader="tableHeader" :tableData="tableData" class="mt10" :isLoading="isLoading" />
      </el-card>
    </div>
    <el-card shadow="never" :body-style="{ padding: '0px 5px 5px' }">
      <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted, ref, watch } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'ConsumablesInStorage'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import Find from '@/components/Find/index.vue'
import fileRequest from '@/utils/fileRequest'
import service from '@/utils/request'
import { ElMessage } from 'element-plus'
import { debounce } from 'lodash'
const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入入库单号',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '入库原因',
    selectLabel: '',
    selectOptions: [
      {
        optionKey: '1',
        optionValue: '材料入库',
        optionLabel: '材料入库'
      },
      {
        optionKey: '2',
        optionValue: '盘盈调整',
        optionLabel: '盘盈调整'
      },
      {
        optionKey: '3',
        optionValue: '现金入库',
        optionLabel: '现金入库'
      }
    ]
  },
  {
    type: 'date',
    dateLabel: '入库日期',
    dateValue: ''
  },
  {
    type: 'select',
    selectP: '部门',
    selectValue: '',
    selectLabel: '',
    selectOptions: [
      {
        optionKey: '1',
        optionValue: '二楼针1组',
        optionLabel: '二楼针1组'
      },
      {
        optionKey: '2',
        optionValue: '二楼针2组',
        optionLabel: '二楼针2组'
      },
      {
        optionKey: '3',
        optionValue: '技术部',
        optionLabel: '技术部'
      },
      {
        optionKey: '4',
        optionValue: '开发室',
        optionLabel: '开发室'
      },
      {
        optionKey: '5',
        optionValue: '版师组',
        optionLabel: '版师组'
      }
    ]
  },
  {
    type: 'select',
    selectP: '厂商代码 厂商名称',
    selectValue: '',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectP: '分类',
    selectValue: '',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '请输入物料编码',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材料名称',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
])
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '导出') {
    if (!formList.value[2].dateValue) {
      JMessage.warning('请选择入库日期')
      return
    }
    const res = (await fileRequest.post(`/pcOrder/downloadInSlipPart`, {
      deptName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '部门')
        ?.selectValue,
      mtlName: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料名称'
      )?.inputValue,
      mtlNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入物料编号'
      )?.inputValue,
      sendNo: '',
      slipEndDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入库日期'
      )?.dateValue?.[1],
      slipNo: '',
      slipReason: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '入库原因')
        ?.selectValue,
      slipStartDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入库日期'
      )?.dateValue?.[0],
      sysEndDate: '',
      sysStartDate: '',
      typeNo: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分类')
        ?.selectValue?.split('  ')[0],
      vendorName: formList.value.find(
        (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称'
      )?.selectValue,
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}

const tableHeader = ref([
  {
    label: '入库单号',
    prop: 'slipNo',
    width: 120
  },
  {
    label: '入库日期',
    prop: 'slipDate'
  },
  {
    label: '入库原因',
    prop: 'slipReason'
  },
  {
    label: '部门名称',
    prop: 'deptName'
  },
  {
    label: '厂商名称',
    prop: 'vendorName'
  },
  {
    label: '物料编号',
    prop: 'mtlNo'
  },
  {
    label: '材料颜色',
    prop: 'mtlColor'
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    width: 320
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    label: '数量',
    prop: 'qty'
  },
  {
    label: '单价',
    prop: 'price'
  },
  {
    label: '金额',
    prop: 'amt',
    width: '220'
  },
  {
    label: '备注',
    prop: 'remark'
  },
  {
    label: '仓管员',
    prop: 'chiName'
  }
])

const tableData = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 6,
    num: 0
  },
  {
    index: 10,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}

// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/pcOrder/getInPcSlipPart', {
    deptName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '部门')
      ?.selectValue,
    mtlName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料名称'
    )?.inputValue,
    mtlNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入物料编号'
    )?.inputValue,
    sendNo: '',
    slipEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入库日期'
    )?.dateValue?.[1],
    slipNo: '',
    slipReason: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '入库原因')
      ?.selectValue,
    slipStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入库日期'
    )?.dateValue?.[0],
    sysEndDate: '',
    sysStartDate: '',
    typeNo: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分类')
      ?.selectValue?.split('  ')[0],
    vendorName: formList.value.find(
      (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称'
    )?.selectValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.list
  total.value = tableDataList.total
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)

// 责任原因,责任人 临时保存变量
const dutyReks = ref([])
const dutyUseres = ref([])
const getSelectList = async () => {
  let vendorName = await service.get<any, any>('/back/vendorQuery')
  let { dutyRek, dutyUsers } = await service.get<any, any>('/back/duty/getDutyCondition')
  dutyReks.value = dutyRek
  dutyUseres.value = dutyUsers
  let { molds } = await service.get<any, any>('/back/order/allOrderCondition')
  let vendorNames = formList.value.find(
    (item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称'
  ) as any
  let category = formList.value.find(
    (item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分类'
  ) as any
  category.selectOptions?.push(
    {
      optionKey: '办公/生产用品类',
      optionValue: `p1  办公/生产用品类`
    },
    {
      optionKey: '机台/固定资产类',
      optionValue: `p2  机台/固定资产类`
    },
    {
      optionKey: '机修/零配件类',
      optionValue: `p3  机修/零配件类`
    },
    {
      optionKey: '胶药水类',
      optionValue: `p4  胶药水类`
    },
    {
      optionKey: '楦头类',
      optionValue: `p5  楦头类`
    },
    {
      optionKey: '网板类',
      optionValue: `p6  网板类`
    },
    {
      optionKey: '模具类',
      optionValue: `p7  模具类`
    },
    {
      optionKey: '斩刀类',
      optionValue: `p8  斩刀类`
    }
  )
  // 插入厂商代码 厂商名称
  if (vendorName && vendorNames) {
    vendorName.forEach((item: any, index: any) => {
      if (item === null) return
      vendorNames.selectOptions?.push({
        optionKey: index,
        optionValue: `${item.vendorNo} ${item.vendorName}`
      })
    })
  }
}
onMounted(() => {
  getSelectList()
})
</script>

<style lang="less" scoped>
:deep(.el-form-item:nth-child(5)) {
  .el-select {
    width: 200px;
    height: 28px;
  }
}

:deep(.el-form-item:nth-child(6)) {
  .el-select {
    width: 200px;
    height: 28px;
  }
}

// 主体样式

.ProductionDynamicMain {
  margin-top: 10rem;
}

.ProductionDynamicMainCardBottom {
  width: 98%;
  height: 110px;
  margin: auto;
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}

.MainLeftCardH1 {
  font-size: 16px;
}

.ProductionDynamicMainCardBottomBtn {
  margin-top: 10px;
}

.ProductionDynamicMainCardHeader {
  width: 100%;
  height: 50px;
  display: flex;

  .ProductionDynamicMainCardHeaderLeft {
    width: 70%;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-around;
  }

  .ProductionDynamicMainCardHeaderRight {
    width: 30%;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-around;
  }
}

// 底部
.bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 40px;

  .total {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.6);
    font-family: PingFangSC-Regular-, PingFangSC-Regular;
  }

  :deep(.el-input__wrapper) {
    padding: 0;
  }

  :deep(.el-input__suffix) {
    transform: translateX(-9px);
  }

  :deep(.el-pagination .el-select .el-input) {
    width: 88px;
  }

  :deep(.number) {
    border: 1px solid #dcdcdc;
    margin-right: 8px;
    border-radius: 3px 3px 3px 3px;
  }

  :deep(.is-active.number) {
    background-color: #f89027 !important;
    color: white;
    border-radius: 3px 3px 3px 3px;
  }

  .save {
    color: black;
    border: 1px solid #e7e7e7;
    background: #e7e7e7;
  }
}
</style>
